package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.FileMapper;
import com.itheima.pojo.*;
import com.itheima.service.BlogService;
import com.itheima.service.CommentService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

   /* @Autowired
    private FileMapper fileMapper;*/

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    /**
     * 分页+条件查询
     * @param commentVO
     * @return
     */
    @RequestMapping("/getList")
    public Map<String,Object> getList(@RequestBody CommentVO commentVO){

        PageInfo pageInfo = commentService.getList(commentVO);

        //封装data
        Map<String,Object> data=new HashMap<>();
        //封装查询数据
        List list = pageInfo.getList();
        List<Map> records=new ArrayList<>();
        for (Object o : list) {
            Map<String,Object> map = JSON.parseObject(JSON.toJSONString(o), Map.class);
            if (map.get("userUid")!=null){
                //根据userUid获取user
                User user = userService.getUserByUid((String) map.get("userUid"));
                Map userMap = JSON.parseObject(JSON.toJSONString(user), Map.class);
                //String url1 = fileMapper.getUrlByUserUid(user.getUid());
                // userMap.put("photoUrl",url1);
                  map.put("user",userMap);
            }

            if (map.get("toUserUid")!=null) {
                //根据toUserUid获取toUser
                User toUser = userService.getUserByUid((String) map.get("toUserUid"));
                Map toUserMap = JSON.parseObject(JSON.toJSONString(toUser), Map.class);
              //String url2 = fileMapper.getUrlByUserUid(toUser.getUid());
               //toUserMap.put("photoUrl", url2);
                map.put("toUser",toUserMap);
            }
            //根据blogUid获取blog
            Blog blog = blogService.findBlogByUid((String) map.get("blogUid"));
            map.put("blog",blog);
            records.add(map);
        }

        data.put("records",records);
        data.put("total",pageInfo.getTotal());
        data.put("size",pageInfo.getSize());
        data.put("current",pageInfo.getPageNum());
        data.put("orders",null);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        //将将data放入返回map
        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","success");
        result.put("data",data);

        return result;
    }

    /**
     * 删除
     * @param commentVO
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody CommentVO commentVO){
        try {
            commentService.delete(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        return new Result(StatusCode.OK,"删除成功");
    }

    /**
     * 删除选中
     * @param commentVOList
     */
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<CommentVO> commentVOList){

        try {
            for (CommentVO commentVO : commentVOList) {
                commentService.deleteBatch(commentVO);
            }
        } catch (Exception e){
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        //封装返回对象
        return new Result(StatusCode.OK,"删除成功");
    }

    /**
     * 添加
     * @param commentVO
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CommentVO commentVO){
        try {
            commentService.add(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"插入失败");
        }
       return new Result(StatusCode.OK,"插入成功");
    }

    /**
     * 编辑
     * @param commentVO
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CommentVO commentVO){
        try {
            commentService.edit(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
        return new Result(StatusCode.OK,"更新成功");
    }
}
